<?php

namespace App\Http\Services;

use App\Models\Member;
use App\Models\Picture;
use App\Models\PictureMapping;
use Carbon\Carbon;

/**
 * 会员
 * @package App\Http\Services
 */
class MemberService
{
    public function __construct()
    {

    }

    /**
     * 检查相册
     */
    public function checkAlbumQrurl()
    {
        $pictures = Picture::where(['qrcode_state' => 1])
            ->where("path", "not like", "http%")
            ->limit(50)
            ->orderBy("id", "desc")
            ->get(['id', 'path'])
            ->toArray();

        foreach ($pictures as $k => $v) {
            file_put_contents("/tmp/check_qrcode_ablumn.txt", var_export($v['id'] . '-' . "picture-" . $v['path'] . '-' . date("Y/m/d H:i:s"), true) . "\n", 8);
            $qrurl = check_qrcode($v['path'], 'ablumn' . $v['id']);

            if (!$qrurl) {
                echo $v['id'] . '<br>';
                Picture::where("id", "=", $v['id'])->update(['qrcode_state' => 2, "qrcode_url" => ""]);
                PictureMapping::where("picture_id", "=", $v['id'])->update(['qrcode_url' => ""]);
                continue;
            }

            echo "picture_id : " . $v['id'] . ', qrcode_url : ' . $qrurl;
            Picture::where("id", "=", $v['id'])->update(['qrcode_state' => 2, 'qrcode_url' => $qrurl]);
            PictureMapping::where("picture_id", "=", $v['id'])->update(['qrcode_url' => $qrurl]);
        }
    }

    /**
     * 检查头像
     */
    public function checkAvatarQrurl()
    {
        $members = Member::where(['avatar_qrcode_state' => 1])
            ->where("avatar", "<>", "")
            ->where("avatar", "not like", "http%")
            ->limit(50)
            ->orderBy("id", "desc")
            ->get(['id', 'avatar'])->toArray();
        $total = Member::where(['avatar_qrcode_state' => 1])->count();
        foreach ($members as $k => $v) {
            file_put_contents("/tmp/check_qrcode_avatar.txt", var_export("avatar-" . $v['avatar'] . '-' . $v['id'] . '-' . date("Y/m/d H:i:s"), true) . "\n", 8);
            if (is_str_has($v['avatar'], 'wxfile://')) {
                Member::where("id", "=", $v['id'])->update([
                    'avatar_qrcode_url' => "",
                    'avatar_qrcode_state' => 2,
                ]);
                continue;
            }

            $qrurl = check_qrcode($v['avatar'], 'avatar' . $v['id']);
            if ($qrurl === false) {
                echo $v['id'] . '<br>';
                $m_data['avatar_qrcode_url'] = "";
            } else {
                $m_data['avatar_qrcode_url'] = $qrurl;
            }

            $m_data['avatar_qrcode_state'] = 2;
            Member::where("id", "=", $v['id'])->update($m_data);
        }
    }

    public function checkWxQrcode()
    {
/*        $url = "/Uploads/wxqrcode/20230208/a23f0d3910bc8f1473be908dcf5ba78e.jpg";
        $res = check_img_qr_code($url, 'wx_qrcode_pic_1');
        dd($res);

        die();*/
        $members = Member::where(['is_run_task' => 0])
            ->where("weixin_qrcode_pic", "<>", "")
            ->limit(100)
            ->orderBy("id", "desc")
            ->get(['id', 'weixin_qrcode_pic'])
            ->toArray();


        $total = Member::where(['is_run_task' => 1])->count();
        echo $total . '<br />';

        foreach ($members as $k => $v) {
            if (empty($v['weixin_qrcode_pic'])) {
                Member::where("id", "=", $v['id'])->update(['is_run_task' => 1, 'enable_wx_qrcode' => 2]);
                continue;
            }
            $res = check_img_qr_code($v['weixin_qrcode_pic'], 'wx_qrcode_pic' . $v['id']);

            $m_data['is_run_task'] = 1;
            $m_data['enable_wx_qrcode'] = $res == true ? 1 : 2;
            Member::where("id", "=", $v['id'])->update($m_data);
        }
    }

}
